23 #define D(x) cout << #x " = " << (x) << endl
25 typedef unsigned long long Int
;
26 const int BUFSIZE
= 256;
34 bool end
; //is this node a complete prefix?
35 node(bool e
) : end(e
) {
40 //not used, because I love to waste memory (makes me feel sexy)
42 if (n
== NULL
) return;
43 clean(n
->left
); clean(n
->right
);
47 Int
f(string s
, node
* n
){ //s = string built so far, n = the node we are standing at
48 Int left
= 0, right
= 0, ret
= 0;
50 left
= f(s
+ "0", n
->left
);
52 if (n
->right
!= NULL
){
53 right
= f(s
+ "1", n
->right
);
59 if (M
- s
.size() < 64){ //we are safe from overflow
60 Int x
= ((1ULL) << (M
- s
.size())); //2^(m - |s|)
61 howmany
= x
- left
- right
;
62 }else{ //2^64 overflows!
64 x
= ~x
; // (x = ~0ULL) <-> (x = 2^64 - 1)
65 howmany
= x
- left
- right
+ 1;
75 while (scanf("%d%d\n", &n
, &M
)==2 && !(n
== 0 && M
== 0)){
77 node
* root
= new node(true);
87 if (cur
->left
== NULL
) cur
->left
= new node(false);
89 }else if (buf
[i
] == '1'){
90 if (cur
->right
== NULL
) cur
->right
= new node(false);
102 buf
[strlen(buf
)-1] = 0; //delete *
103 Int t
= ans
[string(buf
)];